/*
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * 
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 * 
 * See the License for the specific language governing rights and limitations under the License.
*/

using System;
using System.Runtime.Serialization;
using System.Text;

namespace FBConnectAuth
{
	[Serializable]
	public class FBConnectAuthenticationException : Exception
	{
		public FBConnectAuthenticationException() : base(){}

		public FBConnectAuthenticationException(string message) : base(message) {}
		
		public FBConnectAuthenticationException(string message, Exception innerException) : base(message,innerException) {}

		public FBConnectAuthenticationException(string message, ValidationState reason) : this(message)
		{
			this.Reason = reason;
		}

		protected FBConnectAuthenticationException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
				this.Reason =
					(ValidationState) info.GetValue("FBConnectAuthenticationException_Reason",typeof(ValidationState));
		}

		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("FBConnectAuthenticationException_Reason", this.Reason, typeof(ValidationState));
		}

		#region Properties & Operators
		public static implicit operator string(FBConnectAuthenticationException ex)
		{
			return ex.ToString();
		}
		#endregion

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("FBConnectAuthenticationException: {0}", this.Message);
			sb.AppendFormat(" The facebook session could not be Authenticated. Reason: '{0}'", Reason);

			if (this.InnerException != null)
			{
				sb.AppendFormat(" ---> {0} <---", base.InnerException.ToString());
			}

			if (this.StackTrace != null)
			{
				sb.Append(Environment.NewLine);
				sb.Append(base.StackTrace);
			}

			return sb.ToString();
		}
 
		/// <summary>
		/// The cause of the Authentication error.
		/// </summary>
		public ValidationState Reason
		{
			get; private set;
		}
	}
}